{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 行向量相加\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8057895f-67f3-4dc4-af93-015327de21e6",
   "metadata": {},
   "source": [
    "该代码展示了四种对两个列表中的元素进行逐项相加的方法，结果都是对列表`list1 = [1, 2, 3]`和`list2 = [4, 5, 6]`的逐元素相加操作。\n",
    "\n",
    "1. **列表推导式与`zip`函数**：使用`zip`函数将`list1`和`list2`中的元素配对，依次将每对元素相加，形成一个新的列表：\n",
    "\n",
    "   $$\n",
    "   [x + y \\text{ for } x, y \\text{ in zip(list1, list2)}] = [1 + 4, 2 + 5, 3 + 6] = [5, 7, 9]\n",
    "   $$\n",
    "\n",
    "2. **`map`和`lambda`函数**：使用`map`函数与`lambda`表达式逐项相加，每对元素的计算结果与上述相同：\n",
    "\n",
    "   $$\n",
    "   \\text{map(lambda x, y: x + y, list1, list2)} = [5, 7, 9]\n",
    "   $$\n",
    "\n",
    "3. **`numpy`数组直接相加**：将`list1`和`list2`转换为`numpy`数组，然后使用`x + y`进行逐项相加。`numpy`支持数组间的逐项运算，结果与前述相同：\n",
    "\n",
    "   $$\n",
    "   x + y = [5, 7, 9]\n",
    "   $$\n",
    "\n",
    "4. **`numpy.add`函数**：使用`numpy`的`add`函数对`list1`和`list2`进行逐项相加。`np.add`是一种矢量化的相加操作，与直接相加相同，结果为：\n",
    "\n",
    "   $$\n",
    "   \\text{np.add(list1, list2)} = [5, 7, 9]\n",
    "   $$\n",
    "\n",
    "\n",
    "\n",
    "这段代码展示了在Python中对列表元素逐项相加的不同方法，包括纯Python方法和`numpy`方法。使用`numpy`方法尤其适合大规模数据计算，因为`numpy`的运算经过优化，适合处理数组和矩阵等大数据。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60566858-8d51-4513-a4ab-91026ec4ca48",
   "metadata": {},
   "source": [
    "## 使用不同方法对两个列表元素进行逐项相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "97f3cbfe-87ef-4656-b171-2c1de3fde533",
   "metadata": {},
   "outputs": [],
   "source": [
    "list1 = [1, 2, 3]  # 定义第一个列表\n",
    "list2 = [4, 5, 6]  # 定义第二个列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5220c1da-0c78-4930-bc28-d66999d97b2d",
   "metadata": {},
   "source": [
    "## 使用列表推导式和zip函数逐项相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aa4c6fb6-bf42-4ba4-92c6-d096cc11fc2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "print([x + y for x, y in zip(list1, list2)])  # 输出列表推导式计算结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b192d8e-90d2-46ef-93db-54f99adf6a26",
   "metadata": {},
   "source": [
    "## 使用map和lambda函数逐项相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f8d6640c-5592-4f80-b57b-daec7131e648",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "print(list(map(lambda x, y: x + y, list1, list2)))  # 输出map和lambda函数计算结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab5914f9-c6ee-45d1-9e2c-57703d29623c",
   "metadata": {},
   "source": [
    "## 使用numpy对两个数组逐项相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7cf9d235-cd58-4a9d-bccb-8af021c79e28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np  # 导入numpy库\n",
    "\n",
    "x = np.array(list1)  # 将list1转换为numpy数组x\n",
    "y = np.array(list2)  # 将list2转换为numpy数组y\n",
    "print(x + y)  # 使用numpy数组直接逐项相加并输出结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8da81a42-1cf4-4553-8d17-77f441cec920",
   "metadata": {},
   "source": [
    "## 使用numpy的add函数对两个列表逐项相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "99a034a9-2d2f-4f4a-b008-2c391cba2a13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n"
     ]
    }
   ],
   "source": [
    "print(np.add(list1, list2))  # 使用numpy的add函数进行相加并输出结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
